//
//  MSHomeViewController.m
//  testMasory
//
//  Created by xiangbo on 2018/9/19.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSHomeViewController.h"

#import "MSHomeViewSource.h"

#import "MSSearchViewController.h"

@interface MSHomeViewController ()

@property (nonatomic, strong)MSHomeViewSource *viewSource;

@end

@implementation MSHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewSource.headView];
    [self.view addSubview:self.viewSource.homeView];
    
    [self.viewSource.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(110);
    }];
    
    [self.viewSource.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewSource.headView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-44);
    }];
    [self.viewSource.homeView.tableview.mj_header beginRefreshing];
    
    [self initNotification];
}

- (MSHomeViewSource *)viewSource{
    if (!_viewSource) {
        _viewSource = [[MSHomeViewSource alloc] init];
    }
    return _viewSource;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    if (self.viewSource) {
        [self.viewSource refreshHomeLocalTitle];
    }
}

- (void)initNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLocalCityChangedNty:) name:@"locationCityChangedNty" object:nil];
}

- (void)removeNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"locationCityChangedNty" object:nil];
}

#pragma mark - Notifications
/** 收到本地城市信息修改的通知 */
- (void)receiveLocalCityChangedNty:(NSNotification *)notification {
    if (self.viewSource) {
        [self.viewSource refreshHomeLocalTitle];
    }
    [self removeNotification];
}

#pragma mark - Private Method
/** 跳转到搜索界面 */
- (void)presentToSearchViewController {
    MSSearchViewController *searchVC = [MSSearchViewController loadSearchViewControllerWithKind:1 title:@""];
    MSNavigationController *nav = [[MSNavigationController alloc] initWithRootViewController:searchVC];
    [self presentViewController:nav animated:YES completion:nil];
}


@end
